<template>
	<view class="content">
		<swiper class="swiper" :indicator-dots="false" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, index) in bannerList" :key="index">
				<view class="swiper-item">
					<image class="swiper-img" :src="item.image" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>

		<swiper vertical class="swiper-notice" :indicator-dots="false" :autoplay="autoplay" :interval="3000"
			:duration="1000" circular>
			<swiper-item @click="handleNoticeClick(item)" v-for="(item, index) in noticeList" :key="index">
				<view class="swiper-notice-item fs-ellipsis">
					{{ item.content }}
				</view>
			</swiper-item>
		</swiper>

		<view class="fs-flex fs-justify__between fs-p24 fs-full" style="box-sizing: border-box">
			<view @click="navReportingPage" class="nav-item" hover-class="hover-common">
				<image style="width: 100%; height: 100%" src="/static/bd.png" mode="aspectFill"></image>
			</view>
			<view @click="navListPage" class="nav-item" hover-class="hover-common">
				<image style="width: 100%; height: 100%" src="/static/tj.png" mode="aspectFill"></image>
			</view>
		</view>

		<view class="footer-fixed">
			<view v-if="token" class="">
				<fui-button @click="handleLogout" type="link" text="退出登录" color="#465CFF"></fui-button>
			</view>
			<view class="copyright">
				<text @click="contact('18599719996')">新疆幻思信息科技有限公司技术支持</text>
			</view>
			<fui-safe-area background="#f2f2f2"></fui-safe-area>
		</view>

		<fui-modal :show="show" title="消息通知" :descr="descr" :buttons="buttons" maskClosable
			@click="onClick"></fui-modal>
	</view>
</template>

<script setup>
	import { onMounted, ref } from 'vue';
	import { useUserStore } from '../../stores/user';
	import { fetchIndexData } from '@/common/api/index.js';
	import { onShareAppMessage } from '@dcloudio/uni-app'

	const list = ['https://lanrenao.com/wp-content/uploads/2022/05/2025042017323373.gif'];

	function contact(phoneNumber = '') {
		uni.makePhoneCall({
			phoneNumber: phoneNumber
		});
	}

	const { token, setToken } = useUserStore();

	function navReportingPage() {
		if (!token) {
			uni.navigateTo({
				url: '/pages/login/login'
			});
			return;
		}
		uni.navigateTo({
			url: '/pages/reporting/reporting'
		});
	}

	function navListPage() {
		if (!token) {
			uni.navigateTo({
				url: '/pages/login/login'
			});
			return;
		}

		uni.navigateTo({
			url: '/pages/list/list'
		});
	}

	function handleLogout() {
		setToken();
		uni.clearStorageSync();
		uni.reLaunch({
			url: '/pages/login/login'
		});
	}

	const autoplay = ref(true);
	const bannerList = ref([]);
	const noticeList = ref([]);
	async function getIndexData() {
		const { success, data } = await fetchIndexData();
		if (success) {
			bannerList.value = data.banners;
			noticeList.value = data.notices;
		}
	}

	onMounted(() => {
		getIndexData();
	});

	const buttons = [{
		text: '确定',
		color: '#fff',
		background: '#465CFF',
		plain: false
	}];
	const show = ref(false);
	const descr = ref('');

	function showModal() {
		autoplay.value = false;
		show.value = true;
	}

	function cancelModal() {
		autoplay.value = true;
		show.value = false;
	}

	function onClick(e) {
		if (e.index === 0) {
			cancelModal();
		}
	}

	function handleNoticeClick(item) {
		descr.value = item.content;
		showModal();
	}

	onShareAppMessage(() => {
		return {
			title: '捷报Hub'
		}
	})
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		min-height: 100vh;
	}

	.swiper {
		width: 100%;
		height: 200px;
	}

	.swiper-notice {
		width: 100%;
		height: 80rpx;
	}

	.swiper-item {
		width: 100%;
		height: 200px;
	}

	.swiper-notice-item {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		padding: 0 32rpx;
		box-sizing: border-box;
		font-size: 28rpx;
		background: rgba(255, 43, 43, 0.05);
		color: #ff2b2b;
	}

	.swiper-img {
		width: 100%;
		height: 200px;
	}

	.nav-item {
		width: 340rpx;
		height: 200rpx;
		overflow: hidden;
		border-radius: 16rpx;
	}
</style>